<?php
namespace app\api\controller\zfcom;
use x\my\Captcha;
use x\G;
class Comcodephone extends Base
{
	//获取验证码
    public function getcode()
    {
        $d = $this->post;
        $rule = [
            'phone'=>'require|mobile',
        ];
        $msg = [];
        check($rule,$msg,$d);
        $s       = m('Sms','xcom');
        $phone  = $d['phone'];
        $code    = rand(1000,9999);
        $type    = $d['type'];
        $ext = isset($d['ext']) ? $d['ext'] : '';
        switch ($type) {
            #用户注册
            case 'register':
                // if(!$ext){
                //     err('请填写用户名称');
                // }
                // $rs = $this->db->query('select id from user where username=?',[$ext],1);
                // if($rs){
                //     err('用户名已存在!');
                // }
                $ext_arr = explode(',',$ext);
                $img_token = $ext_arr[1];
                $img_code = $ext_arr[0];
                if( !Captcha::checkCaptcha($img_token,$img_code) ){
                    err('图形验证码错误!');
                }
                $rs = $this->db->query('select id from user where phone=?',[$phone],1);
                if($rs){
                    err('该手机号已存在!');
                }
                $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                $no = c('api.yunpian_no');
                break;
            #用户注册 - 小程序
            case 'bind':
                $ext_arr = explode(',',$ext);
                $img_token = $ext_arr[1];
                $img_code = $ext_arr[0];
                if( !Captcha::checkCaptcha($img_token,$img_code) ){
                    err('图形验证码错误!');
                }
                // $rs = $this->db->query('select id from user where phone=?',[$phone],1);
                // if($rs){
                //     err('该手机号已存在!');
                // }
                $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                $no = c('api.yunpian_no');
                break;
            #忘记密码
            case 'forget':
                $ext_arr = explode(',',$ext);
                $img_token = $ext_arr[1];
                $img_code = $ext_arr[0];
                if( !Captcha::checkCaptcha($img_token,$img_code) ){
                    err('图形验证码错误!');
                }
                $rs = $this->db->query('select id from user where phone=?',[$phone],1);
                if(!$rs){
                    err('该手机号不存在!');
                }
                $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                $no = c('api.yunpian_no');
                break;
            #绑定新号码
            case 'newtel':
                $ext_arr = explode(',',$ext);
                $img_token = $ext_arr[1];
                $img_code = $ext_arr[0];
                if( !Captcha::checkCaptcha($img_token,$img_code) ){
                    err('图形验证码错误!');
                }
                // $rs = $this->db->query('select id from user where phone=?',[$phone],1);
                // if($rs){
                //     err('该手机号已存在!');
                // }
                $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                $no = c('api.yunpian_no');
                break;
            #添加银行卡
            case 'bank':
                $ext_arr = explode(',',$ext);
                $img_token = $ext_arr[1];
                $img_code = $ext_arr[0];
                if( !Captcha::checkCaptcha($img_token,$img_code) ){
                    err('图形验证码错误!');
                }
                $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                $no = c('api.yunpian_no');
                break;
            #重置密码
            case 'remake':
                $rs = $this->db->query('select id,password,salt from user where phone=?',[$phone],1);
                if(!$rs){
                    err('该手机号不存在!');
                }
                if(empty($ext)){
                    err('请输入原密码!');
                }
                $password = decode($ext);
                if($rs["password"] != new_com_password($password,$rs['salt'])){
                    err('原密码错误!');
                }
                $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                $no = c('api.yunpian_no');
                break;
            #登录
            case 'login':
                $ext_arr = explode(',',$ext);
                $img_token = $ext_arr[1];
                $img_code = $ext_arr[0];
                if( !Captcha::checkCaptcha($img_token,$img_code) ){
                    err('图形验证码错误!');
                }
                // $rs = $this->db->query('select id from user where phone=?',[$phone],1);
                // if(!$rs){
                //     err('该手机号不存在!');
                // }
                $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                $no = c('api.yunpian_no');
                break;
            case 'smslogin':
                $ext_arr = explode(',',$ext);
                $img_token = $ext_arr[1];
                $img_code = $ext_arr[0];
                if( !Captcha::checkCaptcha($img_token,$img_code) ){
                    err('图形验证码错误!');
                }
                 $rs = $this->db->query('select id from user where phone=?',[$phone],1);
                 if(!$rs){
                     err('该手机号不存在!');
                 }
                $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                $no = c('api.yunpian_no');
                break;
            case 'paypwd':
                $ext_arr = explode(',',$ext);
                $img_token = $ext_arr[1];
                $img_code = $ext_arr[0];
                if( !Captcha::checkCaptcha($img_token,$img_code) ){
                    err('图形验证码错误!');
                }
                $rs = $this->db->query('select id from user where phone=?',[$phone],1);
                if(!$rs){
                    err('该手机号不存在!');
                }
                $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                $no = c('api.yunpian_no');
                break;
            default:
                $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                $no = c('api.yunpian_no');
                break;
        }
        $id = $s->create($phone, $content, $type, $code, $no, $tpl_val);
        if(!$id){
            err($s->getError());
        }
        if($s->sendOne($id)){
            $rs = $this->db->query('select token,code from sms_token where sms_history_id=?',[$id],1);
            suc(['phone_token'=>$rs['token'],'code'=>$rs['code']]);
        }else{
            err($s->getError());
        }
    }
}
